package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //1.判断该商品是否已经存在购物车--条件：用户id+(菜品id+菜品口味id)/套餐id
        //只查当前用户自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        if (cart == null){//代表购物车中没有该商品
            //2.补充缺失的属性值
            // 判断当前是新增菜品还是新增套餐
            if (dto.getDishId() != null){// 代表新增的是菜品
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {// 代表新增的是套餐
                Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //3.将商品数据存入shopping_cart购物车表中
            shoppingCartMapper.insert(shoppingCart);
        }else{// 代表购物车中已经存在该商品
            //4.将原来的购物车的商品数量+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);

        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //注意！只能查看自己购物车的数据
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
    //注意！只能删除自己购物车数据
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //判断该商品是否已经存在购物车--条件：(菜品id+菜品口味id)/套餐id
        //只查当前用户自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        if (cart != null ){//判断当前购物车中是否有该商品
            if (cart.getNumber() == 1){//代表当前购物车中只有1个该商品，则删除该商品
                shoppingCartMapper.deleteById(cart.getId());//不是userId，删主键id
            }else {//代表当前购物车中有多个该商品，则减少商品数量
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            }
        }
    }
}
